/*
 * MyGWT User Extensions
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.mygwt.ux.client.action;

import net.mygwt.ui.client.event.BaseEvent;
import net.mygwt.ui.client.event.SelectionListener;
import net.mygwt.ui.client.widget.Button;
import net.mygwt.ui.client.widget.Component;
import net.mygwt.ui.client.widget.IconButton;
import net.mygwt.ui.client.widget.ToolItem;
import net.mygwt.ui.client.widget.menu.MenuItem;

/**
 * 
 * @author ggeorg
 * 
 */
public class DefaultActionFactory implements IActionFactory {

  public Component createItem(Action pAction, int pType, int pStyle) {
    switch (pType) {
      case Action.BUTTON:
        return createButton(pAction);
      case Action.TOOLITEM:
        return createToolItem(pAction, pStyle);
      case Action.MENUITEM:
        return createMenuItem(pAction, pStyle);
      case Action.ICONBUTTON:
        return createIconButton(pAction);
    }
    return null;
  }

  /**
   * 
   * @param pAction
   * @param pStyle
   * @return
   */
  protected Button createButton(final Action pAction) {
    Button item = new Button();
    item.setText(pAction.getText());
    item.setToolTip(pAction.getToolTip());
    item.setIconStyle(pAction.getIconStyle());
    item.setEnabled(pAction.isEnabled());
    // item.setSelected(pAction.isChecked());
    item.setVisible(pAction.isVisible());
    item.addSelectionListener(new SelectionListener() {
      public void widgetSelected(BaseEvent pEvent) {
        pAction.execute(pEvent);
      }
    });
    return item;
  }

  /**
   * 
   * @param pAction
   * @param pStyle
   * @return
   */
  protected ToolItem createToolItem(final Action pAction, int pStyle) {
    ToolItem item = new ToolItem(pStyle);
    item.setText(pAction.getText());
    item.setToolTip(pAction.getToolTip());
    item.setIconStyle(pAction.getIconStyle());
    item.setEnabled(pAction.isEnabled());
    // item.setSelected(pAction.isChecked());
    item.setVisible(pAction.isVisible());
    item.addSelectionListener(new SelectionListener() {
      public void widgetSelected(BaseEvent pEvent) {
        pAction.execute(pEvent);
      }
    });
    return item;
  }

  /**
   * 
   * @param pAction
   * @param pStyle
   * @return
   */
  private MenuItem createMenuItem(final Action pAction, int pStyle) {
    MenuItem item = new MenuItem(pStyle);
    item.setText(pAction.getText());
    item.setToolTip(pAction.getToolTip());
    item.setIconStyle(pAction.getIconStyle());
    item.setEnabled(pAction.isEnabled());
    item.setSelected(pAction.isChecked());
    item.setVisible(pAction.isVisible());
    item.addSelectionListener(new SelectionListener() {
      public void widgetSelected(BaseEvent pEvent) {
        pAction.execute(pEvent);
      }
    });
    return item;
  }

  /**
   * 
   * @param pAction
   * @return
   */
  private IconButton createIconButton(final Action pAction) {
    IconButton iconBtn = new IconButton(pAction.getIconStyle());
    iconBtn.setToolTip(pAction.getToolTip());
    iconBtn.setEnabled(pAction.isEnabled());
    iconBtn.setVisible(pAction.isVisible());
    iconBtn.addSelectionListener(new SelectionListener() {
      public void widgetSelected(BaseEvent pEvent) {
        pAction.execute(pEvent);
      }
    });
    return iconBtn;
  }

  /*
   * (non-Javadoc)
   * 
   * @see net.mygwt.ux.client.action.IActionFactory#propertyChange(net.mygwt.ui.client.widget.Component,
   *      net.mygwt.ux.client.action.ActionPropertyChangeEvent)
   */
  public void propertyChange(Component pItem, ActionPropertyChangeEvent pEvent) {
    if (pItem instanceof Button) {
      Button item = (Button) pItem;
      switch (pEvent.getActionProperty()) {
        case ActionProperty.TEXT:
          item.setText((String) pEvent.getNewValue());
          break;
        case ActionProperty.TOOLTIPTEXT:
          item.setToolTip((String) pEvent.getNewValue());
          break;
        case ActionProperty.ICONSTYLE:
          item.setIconStyle((String) pEvent.getNewValue());
          break;
        case ActionProperty.ENABLED:
          item.setEnabled(((Boolean) pEvent.getNewValue()).booleanValue());
          break;
        // case ActionProperty.CHECKED:
        // item.setSelected(((Boolean) pEvent.getNewValue()).booleanValue());
        // break;
        case ActionProperty.VISIBLE:
          item.setVisible(((Boolean) pEvent.getNewValue()).booleanValue());
          break;
      }
    } else if (pItem instanceof ToolItem) {
      ToolItem item = (ToolItem) pItem;
      switch (pEvent.getActionProperty()) {
        case ActionProperty.TEXT:
          item.setText((String) pEvent.getNewValue());
          break;
        case ActionProperty.TOOLTIPTEXT:
          item.setToolTip((String) pEvent.getNewValue());
          break;
        case ActionProperty.ICONSTYLE:
          item.setIconStyle((String) pEvent.getNewValue());
          break;
        case ActionProperty.ENABLED:
          item.setEnabled(((Boolean) pEvent.getNewValue()).booleanValue());
          break;
        case ActionProperty.CHECKED:
          item.setSelected(((Boolean) pEvent.getNewValue()).booleanValue());
          break;
        case ActionProperty.VISIBLE:
          item.setVisible(((Boolean) pEvent.getNewValue()).booleanValue());
          break;
      }
    } else if (pItem instanceof MenuItem) {
      MenuItem item = (MenuItem) pItem;
      switch (pEvent.getActionProperty()) {
        case ActionProperty.TEXT:
          item.setText((String) pEvent.getNewValue());
          break;
        case ActionProperty.TOOLTIPTEXT:
          item.setToolTip((String) pEvent.getNewValue());
          break;
        case ActionProperty.ICONSTYLE:
          item.setIconStyle((String) pEvent.getNewValue());
          break;
        case ActionProperty.ENABLED:
          item.setEnabled(((Boolean) pEvent.getNewValue()).booleanValue());
          break;
        case ActionProperty.CHECKED:
          item.setSelected(((Boolean) pEvent.getNewValue()).booleanValue());
          break;
        case ActionProperty.VISIBLE:
          item.setVisible(((Boolean) pEvent.getNewValue()).booleanValue());
          break;
      }
    } else if (pItem instanceof IconButton) {
      IconButton item = (IconButton) pItem;
      switch (pEvent.getActionProperty()) {
        case ActionProperty.TEXT:
          break;
        case ActionProperty.TOOLTIPTEXT:
          item.setToolTip((String) pEvent.getNewValue());
          break;
        case ActionProperty.ICONSTYLE:
          item.changeStyle((String) pEvent.getNewValue());
          break;
        case ActionProperty.ENABLED:
          item.setEnabled(((Boolean) pEvent.getNewValue()).booleanValue());
          break;
        case ActionProperty.CHECKED:
          break;
        case ActionProperty.VISIBLE:
          item.setVisible(((Boolean) pEvent.getNewValue()).booleanValue());
          break;
      }
    }
  }
}
